home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / newlooklib.lha / newlook / createitext.c < prev    next >
C/C++ Source or Header  |  1993-10-24  |  3KB  |  124 lines

  1. /*
  2.  *  CREATEITEXT.C
  3.  */
  4.  
  5. #include "newlook.h"
  6.  
  7. struct TextAttr Topaz80= {
  8.   (STRPTR) "topaz.font",  /* ta_Name  */
  9.   8,                      /* ta_YSize */
  10.   FS_NORMAL,              /* ta_Style */
  11.   FPF_ROMFONT,            /* ta_Flags */
  12. };
  13.  
  14. /*
  15.    strgen(s,t,u) copies all non-underscore characters in `s' to `t' and
  16.    the underscore characters (if any) to `u'.  A space character will be
  17.    written to `u' for each non-underscore character in `s'.
  18.    The #of space characters before the first underscore in `u' will be
  19.    returned.
  20. */
  21.  
  22. static int strgen(s,t,u)
  23. register char *s,*t,*u;
  24. {
  25.   register char *v=u;
  26.   register int n=0;
  27.  
  28.   do
  29.   { if(*s!='_')
  30.     { *t++= *s;
  31.       n++;
  32.     }
  33.     else /* *s=='_' */
  34.     { for(;n>0;--n,*u++=' ');
  35.       *u++= *s;
  36.       n= -1;
  37.     }
  38.   } while(*s++);
  39.   *u= '\0';
  40.  
  41.   for(u=v; *u==' '; u++);
  42.   return (int)(u-v);
  43. }
  44.  
  45.  
  46. struct IntuiText *CreateIText(x,y,text)
  47. SHORT x,y;
  48. STRPTR text;
  49. {
  50.   struct IntuiText *it;
  51.   ULONG UserHandle= SetNewLookHandle(PRIVATE_HANDLE);
  52.  
  53.   if(it= (struct IntuiText *)SmartAllocate(ITEXTSIZE))
  54.   {
  55.     it->FrontPen   = 1;
  56.     it->BackPen    = 0;
  57.     it->DrawMode   = JAM1;
  58.     it->LeftEdge   = x;
  59.     it->TopEdge    = y;
  60.     it->ITextFont  = &Topaz80;
  61.     it->IText      = (UBYTE *)text;
  62.     it->NextText   = (struct IntuiText *)NULL;
  63.  
  64.     if(text && strchr(text,'_'))
  65.     {
  66.       struct IntuiText *iu;
  67.       STRPTR t,u;  /* text, underscores */
  68.       int n= strlen(text) + 1;  /* plus null-terminal */
  69.  
  70.       if(t= (STRPTR)SmartAllocate(n*sizeof(char)))
  71.       {
  72.         if(u= (STRPTR)SmartAllocate(n*sizeof(char)))
  73.         {
  74.           if(iu= (struct IntuiText *)SmartAllocate(ITEXTSIZE))
  75.           {
  76.             n= strgen(text,t,u);
  77.  
  78.             it->IText= (UBYTE *)t;
  79.             iu->IText= (UBYTE *)&u[n];
  80.  
  81.             iu->FrontPen   = 1;
  82.             iu->BackPen    = 0;
  83.             iu->DrawMode   = JAM1;
  84.             iu->LeftEdge   = x + 8*n;
  85.             iu->TopEdge    = y + 1;
  86.             iu->ITextFont  = &Topaz80;
  87.             iu->NextText   = it;
  88.  
  89.             MakePrivateHandlePublic(UserHandle);
  90.             return iu;
  91.             /* --- exit point for text w/ underscores --- */
  92.           }
  93.         }
  94.       }
  95.       if(UserHandle != PRIVATE_HANDLE)
  96.       { SmartFreeAll(PRIVATE_HANDLE);
  97.         (void)SetNewLookHandle(UserHandle);
  98.       }
  99.     }
  100.     else /* no text or no underscores in it */
  101.     { MakePrivateHandlePublic(UserHandle);
  102.       return it;
  103.       /* --- exit point for text w/o underscores --- */
  104.     }
  105.   }
  106.   return (struct IntuiText *)NULL;
  107. }
  108.  
  109.  
  110. #ifdef TEST
  111. #include <stdio.h>
  112.  
  113. main(int argc, char **argv)
  114. {
  115.   char t[80],u[80];
  116.   int n;
  117.  
  118.   while(--argc>0)
  119.   { n= strgen(*++argv,t,u);
  120.     printf("strgen(\"%s\",\"%s\",\"%s\") = %d\n",*argv,t,u,n);
  121.   }
  122. }
  123. #endif /* TEST */
  124.